<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '商品分类添加' : '商品分类编辑'"
      :visible.sync="info.isshow"
      @close="close"
    >
      <el-form :model="user">
        {{ user }}
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="user.pid" placeholder="请选择">
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in showArr"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="user.catename" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 图片 -->
        <el-form-item label="菜单图标" label-width="120px">
          <upload-img @sendFile="user.img = $event" ref="upload"></upload-img>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="info.isAdd" @click="add"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqcateadd, reqcateinfo, reqcateedit } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import { formClose } from "../../../minxins/index";
export default {
  // 接收info
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      // 5.1初始化表单的数据
      user: {
        pid: "",
        catename: "",
        img: null,
        status: 1,
      },
      imgUrl: "", //图片地址
    };
  },
  computed: {
    ...mapGetters({
      arr: "cate/arr",
    }),
    // 算出来渲染的列表
    showArr() {
      return this.arr.filter((item) => item.id !== this.user.id);
    },
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqArr: "cate/reqArr",
    }),

    // 8.点了取消
    cancel() {
      this.info.isshow = false;
    },
    // 清空弹窗的方法
    clearUser() {
      this.user = {
        pid: "",
        catename: "",
        img: "",
        status: 1,
      };
      this.$refs.upload.setImgUrl("");
    },
    // 5.4点了添加按钮
    add() {
      reqcateadd(this.user).then((res) => {
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          // （2）清空弹窗
          this.clearUser();
          // （3）关闭弹窗
          this.info.isshow = false;
          // （4）添加一条数据，重新请求列表
          // this.$emit("init");
          this.reqArr();
        }
      });
    },
    // 6.6上级菜单发生了改变
    changepid() {
      //  如果上级菜单时 顶级菜单， 那么类型就是目录， 否则，类型就是菜单
      if (this.user.pid == 0) {
        this.user.type = 1;
      } else {
        this.user.type = 2;
      }
    },
    // 8.编辑逻辑
    getOne(id) {
      // console.log("发请求"+id);
      reqcateinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.user = res.data.list;
          // 补id
          this.user.id = id;
          this.$refs.upload.setImgUrl(this.$pre + this.user.img);
        }
      });
    },
    // 9.点击修改
    update() {
      reqcateedit(this.user).then((res) => {
        if (res.data.code == 200) {
          // ①弹成功
          successAlert(res.data.msg);
          // ②弹框内容清空
          this.clearUser();
          // ③弹框消失
          this.info.isshow = false;
          // ④重新请求列表
          // this.$emit("init");
          this.reqArr();
        }
      });
    },
    // 10.关闭弹框
    close() {
      if (!this.info.isAdd) {
        this.clearUser();
      }
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
.box {
  position: relative;
}
.el-upload--picture-card {
  position: absolute;
  left: 0;
  top: 0;
  z-index: -1;
}
</style>